/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.delivery.dao;

import com.mycompany.delivery.entities.Contact;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Jana Nemcova
 */
@Repository(value = "contactDao")
public class ContactDAOImpl implements ContactDAO{  
  @PersistenceContext(unitName="TestPU")
  private EntityManager manager;

    public EntityManager getManager() {
        return manager;
    }

    public void setManager(EntityManager manager) {
        this.manager = manager;
    }


  @Override
   public void createContact(Contact contact)
    {
        try
        {
            manager.persist(contact);
        }
        catch (NullPointerException | IllegalArgumentException e) 
        {
            throw new DataAccessException("Problem with argumentL" + e.getMessage()) {
            };
        }
        catch (Exception ex) 
        {
            throw new DataAccessException(ex.getMessage()) {
            };
        }
    }


  @Override
    public void updateContact(Contact contact) 
    {
        try
        {
            manager.merge(contact);
        }
        catch (NullPointerException | IllegalArgumentException e) 
        {
            throw new DataAccessException("Problem with argumentL" + e.getMessage()) {
            };
        }
        catch (Exception ex) 
        {
            throw new DataAccessException(ex.getMessage()) {
            };
        }
    }


  @Override
    public void deleteContact(Contact contact) 
    {
        try
        {
            manager.remove(manager.find(Contact.class, contact.getId()));
        }
        catch (NullPointerException | IllegalArgumentException e) 
        {
            throw new DataAccessException("Problem with argumentL" + e.getMessage()) {
            };
        }
        catch (Exception ex) 
        {
            throw new DataAccessException(ex.getMessage()) {
            };
        }
    }


  @Override
    public Contact getContactById(Long id)
    {
        Contact contacts;
        try
        {
            contacts = (Contact) manager.find(Contact.class, id);
        }
        catch (NullPointerException | IllegalArgumentException e) 
        {
            throw new DataAccessException("Problem with argumentL" + e.getMessage()) {
            };
        }
        catch (Exception ex) 
        {
            throw new DataAccessException(ex.getMessage()) {
            };
        }
        return (Contact) contacts;
    }

  
  @Override
    public List<Contact> getContactBySurname(String name)
    {     
        
        List<Contact> contacts;
        try
        {
            contacts = manager.createQuery("SELECT c FROM Contact c WHERE c.surname = :cSurname").setParameter("cSurname", name).getResultList();
        }
        catch (NullPointerException | IllegalArgumentException e) 
        {
            throw new DataAccessException("Problem with argumentL" + e.getMessage()) {
            };
        } 
        catch (Exception ex) 
        {
            throw new DataAccessException(ex.getMessage()) {
            };
        }
        return  contacts;
    }
    
  @Override
    public List<Contact> getAllContacts() 
    {
        List<Contact> contacts;
        try
        {
            contacts =  manager.createQuery("SELECT c FROM Contact c").getResultList();
        }
              catch (NullPointerException | IllegalArgumentException e) 
        {
            throw new DataAccessException("Problem with argumentL" + e.getMessage()) {
            };
        } 
        catch (Exception ex) 
        {
            throw new DataAccessException(ex.getMessage()) {
            };
        }
        return  contacts;
    }

}
   

